package com.heer.gymnasium.module.login.views;

import android.text.InputType;
import android.text.Selection;
import android.text.Spannable;
import android.text.TextUtils;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.bigkoo.svprogresshud.SVProgressHUD;
import com.heer.gymnasium.api.ResponseData.ApiResponse;
import com.heer.gymnasium.api.callback.BaseCallback;
import com.heer.gymnasium.R;
import com.heer.gymnasium.base.BaseActivity;
import com.heer.gymnasium.module.login.beans.ForgetPasswordBean;
import com.heer.gymnasium.module.login.beans.VerificationBean;
import com.heer.gymnasium.util.StringUtils;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by heer on 17/3/30.
 */

public class ForgetPassWordActivity extends BaseActivity {
    @BindView(R.id.edit_account)
    EditText editAccount;
    @BindView(R.id.edit_password)
    EditText editPassword;
    @BindView(R.id.edit_password_sure)
    EditText editPasswordSure;
    @BindView(R.id.edit_verification_code)
    EditText editVerificationCode;
    @BindView(R.id.btn_request_code)
    Button btnRequestCode;
    @BindView(R.id.btn_register)
    Button btnRegister;
    @BindView(R.id.btn_visible)
    ImageView btnVisible;

    @Override
    protected void init() {
        super.init();
        setContentView(R.layout.layout_forget_password);
    }

    @Override
    protected void initView() {
        super.initView();
        titleUtils.setTitleText("忘记密码");
        btnVisible.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        // 显示密码
                        editPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                        editPassword.postInvalidate();
                        //切换后将EditText光标置于末尾
                        CharSequence charSequence = editPassword.getText();
                        if (charSequence != null) {
                            Spannable spanText = (Spannable) charSequence;
                            Selection.setSelection(spanText, charSequence.length());
                        }
                        break;
                    case MotionEvent.ACTION_UP:
                        // 隐藏密码
                        editPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                        editPassword.postInvalidate();
                        //切换后将EditText光标置于末尾
                        CharSequence charSequence1 = editPassword.getText();
                        if (charSequence1 != null) {
                            Spannable spanText = (Spannable) charSequence1;
                            Selection.setSelection(spanText, charSequence1.length());
                        }
                        break;
                }
                return false;
            }
        });
    }

    @OnClick({R.id.btn_request_code, R.id.btn_register})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_request_code:
                mAppHelper.apiService().sendVerificationCode(editAccount.getText().toString().trim()).enqueue(new BaseCallback<ApiResponse<VerificationBean>>() {
                    @Override
                    public void onSuccess(Call<ApiResponse<VerificationBean>> call, Response<ApiResponse<VerificationBean>> response) {
                        if (response.isSuccessful() && !response.body().isSccessful()) {
                            new SVProgressHUD(mContext).showErrorWithStatus(response.body().getMsg());
                            return;
                        }
                        new SVProgressHUD(mContext).showSuccessWithStatus("发送成功");
                        Log.e(TAG, "onResponse: " + response.body().getMsg());
                    }

                    @Override
                    public void onFailure(Call<ApiResponse<VerificationBean>> call, Throwable t) {
                        new SVProgressHUD(mContext).showSuccessWithStatus("成功");
                        Log.e(TAG, "onResponse: ");
                    }
                });
                break;
            case R.id.btn_register:
                final String mPhone = editAccount.getText().toString().trim();
                String mPassword = editPassword.getText().toString().trim();
                String mPasswordSure = editPasswordSure.getText().toString().trim();
                String mCode = editVerificationCode.getText().toString().trim();
                if (StringUtils.isMobile(mPhone)) {
                    if (mPassword.length() >= 6 && mPassword.length() <= 18) {
                        mAppHelper.apiService().forgetPassword(mPhone, mPassword, mPasswordSure, mCode).enqueue(new BaseCallback<ApiResponse<ForgetPasswordBean>>() {
                            @Override
                            public void onSuccess(Call<ApiResponse<ForgetPasswordBean>> call, Response<ApiResponse<ForgetPasswordBean>> response) {
                                if (response.isSuccessful() && response.body().isSccessful()) {
                                    if (TextUtils.equals(mPhone, response.body().getData().getPhone())) {
                                        Toast.makeText(mContext, "重置密码成功", Toast.LENGTH_SHORT).show();
                                        finish();
                                    }
                                }
                            }

                            @Override
                            public void onFailure(Call<ApiResponse<ForgetPasswordBean>> call, Throwable t) {
                                super.onFailure(call, t);
                            }
                        });
                    } else {
                        new SVProgressHUD(mContext).showErrorWithStatus(getString(R.string.password_error));
                    }
                } else {
                    new SVProgressHUD(mContext).showErrorWithStatus(getString(R.string.account_error));
                }
                break;
        }
    }
}
